package com.zat.mes.andrmes.dofun.work.vm

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.love.zywanandroid.retrofit.RetrofitManager
import com.love.zywanandroid.retrofit.api.ApiService
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.data.BaseResponse
import com.zat.mes.andrmes.data.WorkWageResult
import com.zat.mes.andrmes.retrofit.callback.MesCallBack
import retrofit2.Call
import retrofit2.Response

/**
 * 工资model
 */
class WageModel(application: Application) : AndroidViewModel(application) {

    private val wageListData: MutableLiveData<BaseResponse<WorkWageResult>> = MutableLiveData()
    private val wageDataFail: MutableLiveData<String> = MutableLiveData()
    private val monthWage: MutableLiveData<BaseResponse<WorkWageResult>> = MutableLiveData()
    fun getWage(): LiveData<BaseResponse<WorkWageResult>> {
        return wageListData
    }

    fun getWageFail(): LiveData<String> {
        return wageDataFail
    }

    fun getMonthWage(): LiveData<BaseResponse<WorkWageResult>> {
        return monthWage
    }

    /**
     * 获取员工工资
     */
    fun loadWage(curPage: Int, pageSize: Int, empId: Long, startDate: String?, endDate: String?) {

        RetrofitManager.apiService(ApiService::class.java)
            .getWorkWage(curPage, pageSize, empId, startDate, endDate)
            .enqueue(object : MesCallBack<BaseResponse<WorkWageResult>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<WorkWageResult>>?,
                    response: Response<BaseResponse<WorkWageResult>>?
                ) {
                    wageListData.postValue(response?.body())
                }

                override fun onFailure(call: Call<BaseResponse<WorkWageResult>>, t: Throwable) {
                    super.onFailure(call, t)
                    wageDataFail.postValue(getApplication<Application>().getString(R.string.net_request_fail))
                }
            })
    }

    /**
     * 获取月工资汇总
     */
    fun loadMonthWage(year: String, empId: Long) {

        RetrofitManager.apiService(ApiService::class.java)
            .getMonthWage(year, empId)
            .enqueue(object : MesCallBack<BaseResponse<WorkWageResult>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<WorkWageResult>>?,
                    response: Response<BaseResponse<WorkWageResult>>?
                ) {
                    monthWage.postValue(response?.body())
                }

                override fun onFailure(call: Call<BaseResponse<WorkWageResult>>, t: Throwable) {
                    super.onFailure(call, t)
                    wageDataFail.postValue(getApplication<Application>().getString(R.string.net_request_fail))
                }
            })
    }


}